#!/bin/sh

#
# Copyright 2024  Odin Kroeger
#
# This file is part of SieveManager.
#
# SieveManager is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# SieveManager is distributed in the hope that it will be useful,
# but WITHOUT ALL WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with SieveManager. If not, see
# <https://www.gnu.org/licenses/>.
#

set -Cefu


#
# Globals
#

if git rev-parse --verify HEAD >/dev/null 2>&1
then against=HEAD
else against=$(git hash-object -t tree /dev/null)
fi
readonly against

files="$(git diff --cached --name-only --diff-filter=d "$against")"
readonly files
[ "$files" ] || exit 0

sources=
for file in $files
do
	case $file in
	(*.py) sources="$sources $file"
	esac
done
readonly sources


#
# Main
#

# Prohibit non-ASCII filenames
if test "$(
	git diff --cached --name-only --diff-filter=A -z "$against" |
	LC_ALL=C tr -d '[ -~]\0' |
	wc -c
)" -ne 0
then
	printf '%s: non-ASCII filename\n' >&2 "${0##*/}"
	exit 1
fi

# Prohibit non-portable filenames
# shellcheck disable=2086
pathchk -Pp $files || [ $? -eq 127 ]

# Check code
if [ "$sources" ]
then make sources="${sources# }" quicklint quickcheck
fi

